﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Configuration
{
    public class TransFormationRadioConfig
    {
        double[,] index;

        public TransFormationRadioConfig(String config)
        {
            String[] configNode = config.Split(new char[] { '|' });
            index = new double[configNode.Length, 4];
            for (int i = 0; i < configNode.Length; i++)
            {
                string configNodeStr = configNode[i];
                String[] configContent = configNodeStr.Split(new char[] { ',' });
                double voltage = double.Parse(configContent[0]);
                string minValueStr = configContent[1];
                string maxValueStr = configContent[2];
                double minValue, maxValue;
                Boolean minParseResult =false;
                Boolean maxParseResult = false;
                minParseResult = Double.TryParse(minValueStr, out minValue);
                if (!minParseResult)
                {
                    minValue = 0.0;
                }
                maxParseResult = Double.TryParse(maxValueStr, out maxValue);
                if (!maxParseResult)
                {
                    maxValue = double.MaxValue;
                }
                

                double value = double.Parse(configContent[3]);
              
                index[i, 0] = voltage;
                index[i, 1] = minValue;
                index[i, 2] = maxValue;
                index[i, 3] = value;
   
                for (int j = 0; j < i && (Math.Abs(index[j, 0] - voltage) < double.Epsilon ) ; j++)
                {

                    if ((index[j, 1] - maxValue > Double.Epsilon) || ((minValue - index[j, 2]) > double.Epsilon))
                    {
                        continue;

                    }
                    else
                    {
                        throw new Exception("配置中，电流变比的功率空间有重合!请检查TransformationRatio配置项目");
                    }
                }

            }

           
        }

        private double getValueFromIndex(double voltage, double data)
        {
            for (int i = 0; i < index.GetLength(0); i++)
            {
                double v = index[i, 0];
                double min = index[i, 1];
                double max = index[i, 2];

                if ( (v-voltage) < double.Epsilon &&  (data - max) < double.Epsilon && (min - data) < double.Epsilon)
                {
                    return index[i, 3];
                }
            }
            return double.NaN;
        }

        public double this[double voltage,double power]
        {
            get
            {
                return getValueFromIndex(voltage, power);
            }
        }

    }
}
